<!--
	作者：offline
	时间：2018-12-10
	描述：防伪码生成
-->
<template>
	<div class="m-view-createAntiCounterfeitionCode m-window">
		<div class="m-condition">
			<el-row :gutter="5">
				<el-col :span="2" style="float:right" v-if="permission == 2&&applyBtnShow">
					<el-button type="primary" @click="applyOverCode()" :disabled="btnDis">加码申请</el-button>
				</el-col>
				<el-col :span="2" style="float:right" v-if="bigCodeShow == true">
					<el-button type="primary" @click="create(2)" :disabled="btnDis">生成大码</el-button>
				</el-col>
				<el-col :span="2" style="float:right" v-if="smallCodeShow == true">
					<el-button type="primary" @click="create(3)" :disabled="btnDis">生成小码</el-button>
				</el-col>
				<el-col :span="2" style="float:right">
					<el-button type="primary" @click="search(1)" :disabled="btnDis">查询</el-button>
				</el-col>
				<!--<el-col :span="2">
					<el-button type="primary" @click="print" :disabled="printDis">打印</el-button>
				</el-col>-->
			</el-row>
			<el-row :gutter="5">
				<el-form
					:model="headerRuleForm"
					ref="headerRuleForm"
					label-width="69px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7">
						<el-form-item label="客户" class="rightItem" v-if="permission == 2">
							<el-select
								value-key="id"
								v-model="selCompanyCode"
								placeholder="请选择客户"
								@change="companyChange"
								:disabled="btnDis"
								ref="content"
								filterable
							>
								<el-option
									v-for="item in comOptions"
									:key="item.id"
									:label="item.companyName"
									:value="item"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="公司" class="rightItem" v-if="permission == 1">
							<el-select
								value-key="id"
								v-model="selCompanyCode"
								placeholder="请选择客户"
								@change="companyChange"
								:disabled="btnDis"
								ref="content"
								filterable
							>
								<el-option
									v-for="item in comOptions"
									:key="item.id"
									:label="item.companyName"
									:value="item"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="供应商" class="rightItem" v-if="permission == 1">
							<el-input
								placeholder="请选择供应商"
								v-model="selCusName"
								clearable
								@focus="showSupplierDialog"
								readonly
								@mouseenter.native="mouseEnter"
								@mouseleave.native="mounseLeave"
							>
								<el-button slot="append" icon="el-icon-search" @click="showSupplierDialog"></el-button>
								<span slot="suffix" @click="inputClearSon" class="m-cleanInput" v-show="inputClearBut">
									<i class="el-input_icon el-icon-circle-close el-input__clear"></i>
								</span>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="permission == 2?6:5">
						<el-form-item label="采购单号" class="rightItem">
							<el-input v-model="purOrdNo" placeholder="请输入采购订单号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="permission == 2?6:5">
						<el-form-item label="订单日期" class="rightItem">
							<el-date-picker
								v-model="beginDate"
								type="date"
								placeholder="开始日期"
								value-format="yyyy-MM-dd"
								:editable="false"
								:clearable="false"
								:picker-options="po1"
							></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="permission == 2?4:3">
						<el-date-picker
							v-model="endDate"
							type="date"
							placeholder="结束日期"
							value-format="yyyy-MM-dd"
							:editable="false"
							:clearable="false"
							:picker-options="po2"
						></el-date-picker>
					</el-col>
				</el-form>
			</el-row>
		</div>
		<div class="m-agTable m-bottom m-top2">
			<agGridComponents
				ref="orderInfoGridComponent"
				:getGridOptions="orerInfoGridOptions"
				:getColumnDefs="orderInfoColumnDefs"
				:getRowSelection="orderInfoRowSelection"
				:isColFit="true"
			></agGridComponents>
		</div>
		<div class="m-page">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>

		<el-dialog
			title="防伪码生成"
			v-if="dialogVisible"
			:visible.sync="dialogVisible"
			width="50%"
			:before-close="handleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<el-form
				:model="ruleForm"
				ref="ruleForm"
				label-width="100px"
				class="demo-ruleForm"
				:rules="rules"
				:inline-message="true"
			>
				<el-row>
					<el-col :span="12">
						<el-form-item label="采购订单号">
							<el-input v-model="ruleForm.purOrdNo" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="物料描述">
							<el-input v-model="ruleForm.matName" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="订单数量">
							<el-input v-model="ruleForm.orderAmount" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="已生码数量">
							<el-input v-model="ruleForm.genCodeAmount" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="可生成数量">
							<el-input v-model="ruleForm.canGenAmount" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="类型">
							<el-radio v-model="ruleForm.type" label="1" disabled>大码</el-radio>
							<el-radio v-model="ruleForm.type" label="2" disabled>小码</el-radio>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item label="型号" prop="model">
							<el-input v-model="ruleForm.model" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="物料名称" prop="labMatName">
							<el-input v-model="ruleForm.labMatName" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="执行标准" prop="standard">
							<el-input v-model="ruleForm.standard" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="产地" prop="productionArea">
							<el-input v-model="ruleForm.productionArea" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="保质期" prop="shelfLife">
							<el-input v-model="ruleForm.shelfLife" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="净含量" prop="weight">
							<el-input v-model="ruleForm.weight" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="生产日期" prop="productDate">
							<el-date-picker
								v-model="ruleForm.productDate"
								type="date"
								value-format="yyyy-MM-dd"
								:picker-options="pickerOptions"
							></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="生产批号" prop="batchCode">
							<el-input v-model="ruleForm.batchCode" maxlength="20"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="质检号" prop="inspectionCode">
							<el-input v-model="ruleForm.inspectionCode" maxlength="20"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="生产数量" prop="amount">
							<el-input v-model="ruleForm.amount" maxlength="11" @keyup.native="check"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="saveDataConfirm" :disabled.sync="disState">保存</el-button>
			</span>
		</el-dialog>

		<el-dialog
			title="防伪码生成信息确认"
			v-if="saveDataConfirmDialog"
			:visible.sync="saveDataConfirmDialog"
			width="50%"
			:before-close="handleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<el-form
				:model="dataConfirmForm"
				ref="dataConfirmForm"
				label-width="100px"
				class="demo-ruleForm"
				:rules="rules"
				:inline-message="true"
			>
				<el-row>
					<el-col :span="12">
						<el-form-item label="生产日期">
							<el-input v-model="dataConfirmForm.productDate" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="生产批号">
							<el-input v-model="dataConfirmForm.batchCode" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="质检号">
							<el-input v-model="dataConfirmForm.inspectionCode" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="生产数量">
							<el-input v-model="dataConfirmForm.amount" disabled></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dataConfirmCancel">取消</el-button>
				<el-button type="primary" @click="saveData" :disabled.sync="disState">确定</el-button>
			</span>
		</el-dialog>
		<!-- 条码信息弹窗 -->
		<barCodeInfoDialog
			v-if="barCodeDialogVisible"
			:barCodeDialogVisible="barCodeDialogVisible"
			:isSmallCode="isSmallCode"
			:mainParams="mainParams"
			:delCodeShow="delCodeShow"
			:supplierInfo="supplierInfo"
			:showAddCodeCol="showAddCodeCol"
			@barCodeClose="barCodeClose"
			@updateBarcode="search"
		></barCodeInfoDialog>
		<!-- 供应商弹窗 -->
		<el-dialog
			title="供应商信息"
			:visible.sync="srmSupplierDialogVisible"
			width="50%"
			:before-close="srmSupplierDialogClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<srmSupplierInfo ref="srmSupplierInfo" :layout="layout" @setSrmSupplierInfo="setSrmSupplierInfo"></srmSupplierInfo>
		</el-dialog>
		<!-- 加码申请弹窗 -->
		<overCodeDialog
			:dialogVisible="dialogOptions.visible"
			@closeDialog="closeDialog"
			@codeTypeChange="applyOverCode"
			:ruleForm="materielInfo"
			:editModel="editModel"
			ref="overCodeDialog"
		></overCodeDialog>
	</div>
</template>

<script>
import _Api from "@/api";
import agGridComponents from "@/components/agGridComponents.js";
import srmSupplierInfo from "@/components/srmSupplierInfo.vue";
import overCodeDialog from "./common/overCodeDialog";
import barCodeInfoDialog from "./common/barCodeInfoDialog";
import supplierInfo from "@/public/supplierInfo.js";
import authInfo from "@/public/authInfo.js";
import subOperation from '@/public/addOperation';
import serviceTime from "@/public/serverTime.js";
import buttonLink from "@/components/buttonLink.js";
import Msg from '@/public/message'
import constant from "@/public/constant";


export default {
	data () {
		return {
			headerRuleForm: {},
			purOrdNo: "",
			beginDate: "",
			endDate: "",
			orerInfoGridOptions: "",
			orderInfoColumnDefs: [],
			barCodeGridOptions: "",
			barCodeColumnDefs: [],
			rowSelection: "single",
			barCode: "",
			printCode: "",
			printState: [
				{ label: "未打印", value: 1 },
				{ label: "已打印", value: 2 }
			],
			orderInfoRowSelection: "single",
			barCodeRowSelection: "multiple",
			dialogVisible: false,
			ruleForm: {},
			codeForm: {},
			rules: {
				model: [{
					required: true
				}],
				labMatName: [{
					required: true
				}],
				standard: [{
					required: true
				}],
				shelfLife: [{
					required: true
				}],
				productDate: [{
					required: true,
					trigger: "change",
					message: "请选择生产日期！"
				}],
				productionArea: [{
					required: true
				}],
				weight: [{
					required: true
				}],
				amount: [{
					required: true,
					trigger: "change",
					message: "生产数量不能为空！"
				}],
				batchCode: [{
					required: true,
					trigger: "change",
					message: "生产批号不能为空！"
				}],
				inspectionCode: [{
					required: true,
					trigger: "change",
					message: "质检号不能为空！"
				}]
			},
			disState: true,
			supplierInfo: [],
			orderRowData: [],
			barCodesRowData: [],
			pickerOptions: {
				// disabledDate (time) {
				//   return time.getTime() < (Date.now() - 3600 * 1000 * 24);
				// }
			},
			btnDis: false,
			selCompanyCode: "",
			comOptions: [],
			printDis: false,
			conDis: false,
			currentPage: 1,
			pageSize: 30,
			total: 0,
			barCodeCurrentPage: 1,
			barCodePageSize: 100,
			barCodeTotal: 0,
			barCodeDialogVisible: false,
			selRow: {},
			current: 0,
			textChange: 0,
			po1: {
				disabledDate: this.disabledDate1
			},
			po2: {
				disabledDate: this.disabledDate2
			},
			//大小码相关
			mainParams: {},
			isSmallCode: false,
			permission: '',
			layout: {},
			srmSupplierDialogVisible: false,
			selCusName: '',
			inputClearBut: false,
			delCodeShow: false,
			smallCodeShow: false,
			bigCodeShow: false,
			codeDeling: false,
			messageType: '',
			barcodeArr: [],

			//生成条码信息确认弹框
			saveDataConfirmDialog: false,
			dataConfirmForm: {},
			// 加码申请弹窗相关
			dialogOptions: {
				visible: false
			},
			// 加码申请物料信息
			materielInfo: {
				purOrdNo: '',
				matName: '',
				orderAmount: '',
				genCodeAmount: '',
				canGenAmount: '',
				type: 1,
				model: '',
				labMatName: '',
				standard: '',
				productionArea: '',
				shelfLife: '',
				weight: '',
				productDate: '',
				batchCode: '',
				inspectionCode: '',
				amount: '',
				remark: '',
				rowData: {}
			},
			// 加码申请弹窗编辑类型  
			editModel: 'add',
			//   显示是否加码列
			showAddCodeCol: true,
			//是否显示加码申请按钮
			applyBtnShow: false
		}
	},
	methods: {
		disabledDate1 (time) {
			if (this.endDate)
				return time.getTime() > new Date(this.endDate).getTime();
		},
		disabledDate2 (time) {
			if (this.beginDate)
				return time.getTime() < (new Date(this.beginDate).getTime() - 3600 * 1000 * 24);
		},
		barCodeClose () {
			this.barCodeDialogVisible = false
		},
		setClickValue (params) {
			var that = this;
			this.isSmallCode = false;
			this.mainParams = params;
			if (that.mainParams.colDef.field == 'genSCodeAmount') {
				this.isSmallCode = true;
			}
			this.barCodeDialogVisible = true;
			this.selRow = params.data;
			//   this.$nextTick(_ => {
			//     that.orderRowSelected(that.selRow, 3);
			//   })
		},
		handleSizeChange (params) {
			this.currentPage = 1;
			this.pageSize = params;
			this.search();
		},
		handleCurrentChange (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
		},
		handleNextClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
		},
		companyChange (params) {
			if (this.permission == 1) {
				if (params) {
					this.layout.companyCode = params.companyCode;
					this.layout.auth = true;
					// this.layout.filterByUser = true;
					if (this.selCusCode)
						this.selCusCode = "";
					if (this.selCusName)
						this.selCusName = "";
				} else {
					this.layout = {};
					this.selCusName = "";
				}
			} else if (this.permission == 2) {
				this.search(1);
			}
		},
		onFilterChanged () {
			var that = this;
			//   var rows = this.$refs.orderInfoGridComponent.gridOptions.api.getSelectedRows();
			//				if(rows.length === 1){}
			this.conDis = true;
			this.barCodeCurrentPage = 1;
			this.barCodePageSize = 30;
			this.$refs.barCodeGridComponent.loading = true;
			var obj = {
				unique: this.selRow.unique,
				currentPage: 0,
				pageSize: 30
			};
			if (this.barCode != "")
				obj.barcode = this.barCode;
			if (this.printCode != "" && this.printCode != null)
				obj.isPrint = this.printCode - 1;

			_Api.getBarCodes(obj).then(function (response) {
				var data = response.data;
				that.$refs.barCodeGridComponent.loading = false;
				that.conDis = false;
				if (data.code == "1") {
					if (data.total > 0) {
						that.$refs.barCodeGridComponent.gridOptions.api.setRowData(data.rows.splice(0));
						that.$refs.barCodeGridComponent.rowDataChange = true;
						that.$refs.barCodeGridComponent.gridOptions.api.forEachNode(function (node) {
							let rowIndex = that.$refs.barCodeGridComponent.gridOptions.api.getRowNode(node.rowIndex);
							if (node.data.printTime === '0')
								rowIndex.setDataValue('printTime', 0);
							else
								rowIndex.setDataValue("printTime", parseInt(node.data.printTime));
						})
						that.barCodeTotal = data.total;
						that.$refs.barCodeGridComponent.rowDataChange = true;
					} else {
						that.$refs.barCodeGridComponent.gridOptions.api.setRowData([]);
						that.barCodeTotal = 0;
					}
				} else {
					that.$refs.barCodeGridComponent.loading = false;
					that.barCodeTotal = 0;
					that.conDis = false;
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				}
			}).catch((err) => {
				that.$refs.barCodeGridComponent.loading = false;
				that.$alert(err.data.message, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
			})

		},
		check (params) {
			if (this.ruleForm.amount) {
				if (parseInt(this.ruleForm.amount.trim()) === 0)
					this.$set(this.ruleForm, 'amount', '');

				this.ruleForm.amount = this.ruleForm.amount.replace(/[^\d]/g, '');
				if (/\d/.test(this.ruleForm.amount)) {
					this.disState = false;
					return;
				} else {
					this.disState = true;
					return;
				}
			}
		},
		search (flag) {
			if (this.beginDate != "" && this.beginDate != null && this.endDate != "" && this.endDate != null) {
				if (this.beginDate > this.endDate) {
					this.$alert("提示：开始日期不能大于结束日期", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return;
				}
			}
			if (this.permission === 2) {
				if (this.selCompanyCode == "") {
					this.$alert("提示：请选择客户！", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return
				}
			}

			if (this.permission === 1) {
				if (this.selCompanyCode == "") {
					this.$alert("提示：请选择公司！", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return
				}

				if (flag === 1) {
					if (this.selCusName == "") {
						this.$alert("提示：请选择供应商！", {
							showClose: false,
							confirmButtonText: "确定",
							customClass: "alertPrompt"
						});
						return
					}
				}
			}
			var obj = {};
			var that = this;

			obj.companyCode = this.selCompanyCode.companyCode;
			if (this.$global.user.userTypeId == "4") {
				obj.supplierCode = this.supplierInfo[0].supplierCode;
			} else if (this.$global.user.userTypeId == "1") {
				obj.supplierCode = this.supplierInfo[0].supplierCode;
			}
			if (this.purOrdNo)
				obj.purOrdNo = this.purOrdNo;
			if (this.beginDate != "" && this.beginDate != null)
				obj.Begin_Date = this.beginDate
			else if ((this.beginDate == "" || this.beginDate == null) && this.endDate != "" && this.endDate != null)
				obj.Begin_Date = this.endDate
			if (this.endDate != "" && this.endDate != null)
				obj.End_Date = this.endDate
			else if ((this.endDate == "" || this.endDate == null) && this.beginDate != "" && this.beginDate != null)
				obj.End_Date = this.beginDate;
			if (flag === 1 || flag === 7)
				this.currentPage = 1;
			obj.current = this.currentPage;//this.currentPage;
			obj.size = this.pageSize;
			this.$refs.orderInfoGridComponent.loading = true;
			this.btnDis = true;

			_Api.getAntiPurList(obj).then(function (response) {
				that.btnDis = false;
				var data = response.data;
				that.current = 0;
				that.textChange = 0;
				that.$refs.orderInfoGridComponent.loading = false;
				if (data.code == "1") {
					if (data.total > 0) {
						that.orderRowData = data.rows.splice(0);
						that.$refs.orderInfoGridComponent.gridOptions.api.setRowData(that.orderRowData);
						that.total = data.total;
						that.$refs.orderInfoGridComponent.rowDataChange = true;
					} else {
						that.orderRowData = [];
						that.$refs.orderInfoGridComponent.gridOptions.api.setRowData([]);
						that.total = 0;
					}
				} else {
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
					that.total = 0;
					that.btnDis = false;
				}
			}).catch(() => {
				that.$refs.orderInfoGridComponent.loading = false;
				that.current = 0;
				that.textChange = 0;
				that.total = 0;
				that.btnDis = false;
				that.$alert(data.info, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
			})
		},
		create (codeFlag) {
			var row = this.$refs.orderInfoGridComponent.gridOptions.api.getSelectedRows();
			var that = this;
			var createCodeType = '';
			if (row.length != 1) {
				this.$alert("请选择需要生成的订单!", {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
				return;
			}
			this.dialogVisible = true;
			this.$set(this.ruleForm, 'purOrdNo', row[0].purOrdNo);
			this.$set(this.ruleForm, 'matCode', row[0].matCode);
			this.$set(this.ruleForm, 'purOrdMxNo', row[0].purOrdMxNo);
			this.$set(this.ruleForm, 'matName', row[0].matName);
			this.$set(this.ruleForm, 'orderAmount', row[0].orderAmount);
			this.$set(this.ruleForm, 'genCodeAmount', row[0].genCodeAmount);
			var canGenAmount = subOperation.accSub(row[0].orderAmount, row[0].genCodeAmount);
			this.$set(this.ruleForm, 'canGenAmount', canGenAmount);
			if (codeFlag === 2) {
				this.$set(this.ruleForm, 'type', '1');
				createCodeType = 1;
				this.$set(this.ruleForm, 'orderAmount', '');
				this.$set(this.ruleForm, 'genCodeAmount', '');
				this.$set(this.ruleForm, 'canGenAmount', '');
			} else if (codeFlag === 3) {
				this.$set(this.ruleForm, 'type', '2');
				createCodeType = 2;
				this.$set(this.ruleForm, 'orderAmount', '');
				this.$set(this.ruleForm, 'genCodeAmount', '');
				this.$set(this.ruleForm, 'canGenAmount', '');
			}
			var obj = {
				proId: row[0].matCode,
				clientId: this.supplierInfo[0].supplierCode,
				type: createCodeType
			};
			_Api.getMaterialInfo(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					if (data.total > 0) {
						that.$set(that.ruleForm, 'model', data.rows[0].XH);//型号
						that.$set(that.ruleForm, 'standard', data.rows[0].executeStandard);//执行标准
						that.$set(that.ruleForm, 'labMatName', data.rows[0].NAME);//标签物料名称
						that.$set(that.ruleForm, 'levelNum', data.rows[0].CJ);//
						that.$set(that.ruleForm, 'assistNum', data.rows[0].FZSL);//辅助数量
						that.$set(that.ruleForm, 'jth', data.rows[0].jth);//机台号
						that.$set(that.ruleForm, 'weight', data.rows[0].netWeight);//净重
						that.$set(that.ruleForm, 'productionArea', data.rows[0].productPlace);//产地
						that.$set(that.ruleForm, 'shelfLife', data.rows[0].qualityDate);//保质期
						that.$set(that.ruleForm, 'matching', data.rows[0].matching);
						//小码：订单数量、可生成和已生成条码数要重新计算
						if (that.ruleForm.type === '2') {
							var sCodeOrderAmount = row[0].orderAmount * data.rows[0].packageAmount;
							var sGenCodeAmount = row[0].genSCodeAmount;
							var sCanCodeAmount = sCodeOrderAmount - sGenCodeAmount;
							that.$set(that.ruleForm, 'orderAmount', sCodeOrderAmount);
							that.$set(that.ruleForm, 'genCodeAmount', sGenCodeAmount);
							that.$set(that.ruleForm, 'canGenAmount', sCanCodeAmount);
							that.$set(that.ruleForm, 'packageAmount', data.rows[0].packageAmount);
							that.$set(that.ruleForm, 'auxAmount', data.rows[0].packageAmount);
						} else if (that.ruleForm.type === '1') {
							var bCodeOrderAmount = row[0].orderAmount * data.rows[0].packageAmount;
							var bGenCodeAmount = row[0].genCodeAmount;
							var bCanCodeAmount = bCodeOrderAmount - bGenCodeAmount;
							that.$set(that.ruleForm, 'orderAmount', bCodeOrderAmount);
							that.$set(that.ruleForm, 'genCodeAmount', bGenCodeAmount);
							that.$set(that.ruleForm, 'canGenAmount', bCanCodeAmount);
							that.$set(that.ruleForm, 'packageAmount', data.rows[0].packageAmount);
							that.$set(that.ruleForm, 'auxAmount', data.rows[0].packageAmount);
						}
					}
				} else {
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				}
			}).catch((err) => {
				that.$alert(err.data.message, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
			})
		},
		printCodeChange (params) {
			this.onFilterChanged();
			//				var filterComponent = this.$refs.barCodeGridComponent.gridOptions.api.getFilterInstance('printTime');
			//				if(params === 1){
			//					filterComponent.setModel({
			//						type:'equals',
			//						filter:0,
			//						filterTo:null
			//					});
			//				}else if (params === 2){
			//					filterComponent.setModel({
			//						type:'greaterThan',
			//						filter:0,
			//						filterTo:null
			//					})
			//				}
			//				this.$refs.barCodeGridComponent.gridOptions.api.onFilterChanged();
		},
		handleClose (done) {
			this.ruleForm = {};
			this.$refs.ruleForm.resetFields();
			this.$refs.ruleForm.clearValidate();
			done();
		},
		cancel () {
			this.ruleForm = {};
			this.$refs.ruleForm.resetFields();
			this.ruleForm = {};
			this.$refs.ruleForm.clearValidate();
			this.dialogVisible = false;
		},
		saveData () {
			var that = this;
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					that.ruleForm.productDate = new Date(that.ruleForm.productDate).Format("yyyy-MM-dd HH:mm:ss");
					that.ruleForm.creatorId = this.$global.user.id;
					that.ruleForm.creator = this.$global.user.employeeName;
					if (that.ruleForm.type === '1') {
						that.ruleForm.type = 1;
					} else if (that.ruleForm.type === '2') {
						that.ruleForm.type = 2;
					}
					that.disState = true;
					_Api.saveBarCode(that.ruleForm).then(function (response) {
						var data = response.data;
						if (data.code == "1") {
							that.$message({
								message: "操作成功",
								type: "success"
							});
							that.dialogVisible = false;
							that.saveDataConfirmDialog = false;
							that.ruleForm = {};
							that.$refs.ruleForm.clearValidate();
							that.search();
						} else {
							if (that.ruleForm.type === 1) {
								that.ruleForm.type = '1';
							} else if (that.ruleForm.type === 2) {
								that.ruleForm.type = '2';
							}
							that.$alert(data.info, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
						}
						that.disState = false;
					}).catch((err) => {
						that.$alert(err.data.message, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
						that.disState = false;
					})
				} else {
					return false;
				}
			})
		},

		saveDataConfirm () {
			this.dataConfirmForm = this.ruleForm;
			this.saveDataConfirmDialog = true;
		},

		dataConfirmCancel () {
			this.saveDataConfirmDialog = false;
		},

		showSupplierDialog () {
			var that = this;
			if (!this.layout.companyCode) {
				this.$alert("提示：请选择公司！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				this.$refs.content.focus();
				return;
			} else {
				this.srmSupplierDialogVisible = true;
				this.$nextTick(_ => {
					that.$refs.srmSupplierInfo.search();
				});
			}
		},

		// 关闭供应商弹窗回调
		srmSupplierDialogClose (done) {
			this.$refs.srmSupplierInfo.keyWord = "";
			this.$refs.srmSupplierInfo.selSupplierOptions.api.setQuickFilter(this.$refs.srmSupplierInfo.keyWord);
			done();
		},

		// 回显选择的供应商信息
		setSrmSupplierInfo (msg) {
			if (msg.length > 0) {
				this.srmSupplierDialogVisible = false;
				this.selCusCode = msg[0].supplierCode;
				this.selCusName = msg[0].supplierName;
				this.supplierInfo[0] = msg[0];
			}
		},

		// 输入框清除事件监听
		inputClearSon () {
			this.selCusCode = "";
			this.selCusName = "";
			this.supplierInfo[0].supplierCode = "";
			this.inputClearBut = false;
		},

		// 供应商输入框鼠标移入事件监听
		mouseEnter () {
			if (this.selCusName != "")
				this.inputClearBut = true;
		},
		// 供应商输入框鼠标移出事件监听
		mounseLeave () {
			if (this.selCusName != "")
				this.inputClearBut = false;
		},
		// 获取操作按钮权限
		getBtAuth () {
			var that = this;
			this.smallCodeShow = false;
			this.bigCodeShow = false;
			this.delCodeShow = false;
			this.applyBtnShow = false;
			var obj = {
				resultType: "0",
				code: 'createAntiCounterfeitionCode',
				authClientCode: constant.authCode.code,
				authTenantCode: constant.authCode.tcode
			};
			_Api.getUserAuthMenu(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					if (data.total > 0) {
						for (let i in data.rows) {
							if (data.rows[i].code == "deleteCode") {
								that.delCodeShow = true;
							} else if (data.rows[i].code == "smallBarCode") {
								that.smallCodeShow = true;
							} else if (data.rows[i].code == "bigBarCode") {
								that.bigCodeShow = true;
							} else if (data.rows[i].code == "barCodeApply") {
								that.applyBtnShow = true
							}
						}

						if (that.smallCodeShow == false) {
							that.$refs.orderInfoGridComponent.gridOptions.columnApi.setColumnVisible(
								"genSCodeAmount",
								false
							);
						}
						if (that.bigCodeShow == false) {
							that.$refs.orderInfoGridComponent.gridOptions.columnApi.setColumnVisible(
								"genCodeAmount",
								false
							);
						}
					} else {
						that.delCodeShow = false;
						that.bigCodeShow = false;
						that.smallCodeShow = false;
						// that.delCodeShow = true
					}
				}
			}).catch((err) => {
				Msg.showErrorMsg(err.data.message);
			})
		},
		// 打开加码申请弹窗
		applyOverCode (codeType) {
			if (!codeType) {
				let row = this.$refs.orderInfoGridComponent.gridOptions.api.getSelectedRows()
				if (row.length != 1) {
					this.$alert("请选择需要申请加码的订单!", {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					})
					return
				}
				this.materielInfo.type = 1
				this.rowData = row[0]
			}
			const { purOrdMxNo, purOrdNo, matCode, matName, orderAmount } = this.rowData
			let params = {
				supplierCode: this.supplierInfo[0].supplierCode,
				type: codeType ? codeType : this.materielInfo.type,
				purOrdMxNo,
				purOrdNo,
				matCode
			}
			this.materielInfo.purOrdNo = purOrdNo //订单号
			this.materielInfo.matName = matName //物料描述
			//   this.materielInfo.genCodeAmount = params.type == 1 ? genCodeAmount : genSCodeAmount //已生成数量
			this.materielInfo.purOrdMxNo = purOrdMxNo //订单行号
			this.materielInfo.matCode = matCode //物料编号
			this.dialogOptions.visible = true
			this.$refs['overCodeDialog'].loading = true
			this.$http.applyCodeMaterielInfo(params).then(({ data }) => {
				this.$refs['overCodeDialog'].loading = false
				if (data.code == 1) {
					const { auxAmount, labMatName, model, productionArea, shelfLife, standard, weight, assistNum, level, jth, excessLimit, genCodeAmount } = data.rows
					this.materielInfo.orderAmount = orderAmount// 订单数量
					let packageAmount = subOperation.accMul(orderAmount, auxAmount)// 包装数量
					let limitAmount = Math.round(subOperation.accMul(packageAmount, excessLimit)) //最大可申请加码数量,四舍五入取整 
					this.materielInfo.genCodeAmount = genCodeAmount //已加码数量
					this.materielInfo.canGenAmount = subOperation.accSub(limitAmount, this.materielInfo.genCodeAmount) //可加码数量
					this.materielInfo.labMatName = labMatName //物料名称
					this.materielInfo.model = model //型号
					this.materielInfo.productionArea = productionArea // 产地
					this.materielInfo.shelfLife = shelfLife //保质期
					this.materielInfo.standard = standard //执行标准
					this.materielInfo.weight = weight //重量
					this.materielInfo.assistNum = assistNum //辅数量
					this.materielInfo.level = level //层级
					this.materielInfo.jth = jth //机台号
					this.materielInfo.auxAmount = auxAmount
					this.materielInfo.excessLimit = excessLimit
					if (this.materielInfo.canGenAmount == 0 || this.materielInfo.canGenAmount < 0) {
						this.$alert('当前物料无可申请加码数量！', '提示', {
							confirmButtonText: '确定',
							callback: _ => {
								// this.closeDialog()
								this.dialogOptions.visible = false
							}
						})
					}
				} else {
					this.$refs['overCodeDialog'].resetForm()
					Msg.showErrorMsg(data.info)
				}
			}).catch(err => {
				this.$refs['overCodeDialog'].resetForm()
				this.$refs['overCodeDialog'].loading = false
				Msg.showErrorMsg(err.data.message)
			})
		},
		closeDialog () {
			this.materielInfo = {
				purOrdNo: '',
				matName: '',
				orderAmount: '',
				genCodeAmount: '',
				canGenAmount: '',
				type: '1',
				model: '',
				labMatName: '',
				standard: '',
				productionArea: '',
				shelfLife: '',
				weight: '',
				productDate: '',
				batchCode: '',
				inspectionCode: '',
				amount: '',
				remark: '',
				auxAmount: ''
			}
			this.dialogOptions.visible = false
		}
	},
	beforeMount () {
		this.barCodeGridOptions = {

		}

		this.orerInfoGridOptions = {
			// onRowSelected:this.orderRowSelected
			context: {
				componentParent: this
			},
		}
		this.orderInfoColumnDefs = [
			{ headerName: "采购订单号", field: "purOrdNo", width: 100, checkboxSelection: true },
			{ headerName: "物料号", field: 'matCode' },
			{ headerName: "物料描述", field: "matName" },
			{ headerName: "已发数量", field: "issuedAmount" },
			{
				headerName: "发货日期",
				field: "deliveryDate",
				cellRenderer: function (params) {
					if (params.value)
						return new Date(params.value).Format("yyyy-MM-dd");
				}
			},
			{ headerName: "订单数量", field: "orderAmount" },
			// {
			//   headerName: "已生成条码数",
			//   field: "genCodeAmount",
			//   cellRenderer: buttonLink
			// },
			{
				headerName: "已生成大码数",
				field: "genCodeAmount",
				// cellRendererFramework: genCodeAmount
				cellRenderer: buttonLink,
			},
			{
				headerName: "已生成小码数",
				field: "genSCodeAmount",
				cellRenderer: buttonLink,
			}];

		this.barCodeColumnDefs = [
			{
				headerName: "序号",
				field: "",
				cellRenderer: function (params) {
					return parseInt(params.node.id) + 1
				}, hide: true
			},
			{
				headerName: "条码",
				field: "",
				checkboxSelection: true,
				headerCheckboxSelection: true,
				headerCheckboxSelectionFilteredOnly: true,
				cellRenderer: function (params) {
					var hideStr = params.data.barcode.substring(4, params.data.barcode.length - 4);
					return params.data.barcode.replace(hideStr, '**********');
				}
			},
			{ headerName: "送货单号", field: "delivery" },
			{
				headerName: "生成日期",
				field: "createDate",
				//   cellRenderer: function (params) {
				//       if (params.value)
				//         return new Date(params.value).Format("yyyy-MM-dd");
				//     }
			},
			{
				headerName: "生产日期",
				field: "produceDate",
				cellRenderer: function (params) {
					if (params.value)
						return new Date(params.value).Format("yyyy-MM-dd");
				}
			},
			{ headerName: "生产批号", field: "batchCode" },
			// {
			//   headerName: "条码",
			//   field: "",
			//   cellRenderer: function (params) {
			//     var hideStr = params.data.barcode.substring(4, params.data.barcode.length - 4);
			//     return params.data.barcode.replace(hideStr, '**********');
			//   }
			// },
			{ headerName: "打印次数", field: "printTime", filter: 'agNumberColumnFilter' }];
	},
	mounted () {
		var that = this;
		//获取按钮权限
		this.getBtAuth();
		serviceTime.getServiceTime().then((serviceTime) => {
			//				that.beginDate = serviceTime.Format("yyyy-MM-dd");
			that.beginDate = new Date((serviceTime.getTime() - 604800000)).Format("yyyy-MM-dd");
			that.endDate = serviceTime.Format("yyyy-MM-dd");
		});
		if (this.$global.user.userTypeId == "4") {
			this.permission = 2;
			this.supplierInfo.push(this.$global.supplierData);
			if (this.supplierInfo[0] == null) {
				supplierInfo.getUserInfo(this.$global.user.userName).then((supplierInfo) => {
					that.supplierInfo = supplierInfo;
				})
			}
			authInfo.getAuthInfo().then((authCompanyInfo) => {
				that.comOptions = authCompanyInfo;
			});
		} else {
			authInfo.getAuthInfo().then((authCompanyInfo) => {
				that.comOptions = authCompanyInfo;
			});
			this.permission = 1;
			// this.btnDis = true;
		}
	},
	components: {
		agGridComponents,
		srmSupplierInfo,
		overCodeDialog,
		barCodeInfoDialog
	},
	watch: {
		selCompanyCode () {
			if (this.permission == 2) {
				this.current = 1;
				this.search();
			}
		},
		purOrdNo (val) {
			//采购订单号
			if (val)
				this.textChange = 1;
			else
				this.textChange = 0;
		},
		beginDate () {
			this.current = 1;
		},
		endDate () {
			this.current = 1;
		}
	}
}
</script>